

package com.base.commons.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.base.commons.mybatis.enums.DelFlagEnum;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 公共字段，自动填充值
 *
 * @author carl
 * @since 1.0.0
 */
@Component
public class FieldMetaObjectHandler implements MetaObjectHandler {
    private final static String CREATE_DATE = "createTime";
    private final static String UPDATE_DATE = "updateTime";
    private final static String DEL_FLAG = "delFlag";



    @Override
    public void insertFill(MetaObject metaObject) {
        Date date = new Date();
        //创建时间
        setFieldValByName(CREATE_DATE, date, metaObject);
        //更新时间
        setFieldValByName(UPDATE_DATE, date, metaObject);
        //删除标识
        if(null == getFieldValByName(DEL_FLAG,metaObject)){
            setFieldValByName(DEL_FLAG, DelFlagEnum.NORMAL.value(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新时间
        setFieldValByName(UPDATE_DATE, new Date(), metaObject);
    }
}